//$.ajax({
//       type     : 'POST',
//       url      : 'dbcheck.php',
//       data     : {dataoption : 'table'},
//       datatype : 'html',
//       success : function(response){
//           $('#productname').append(response);
//       }
//});
function savecustomquestion(){
    $.ajax({
            type : 'POST',
            url : 'customquestion.php',
            data:{tablename:$('#productname').val(),name:$('#uname').val(),email_address:$('#email').val(),voucher_code:$('#voucher_code').val(),question:$('#customquestion').val()},
            success: function(response){
                window.location.replace("http://simple.comeze.com");
            },
            error : function(response){
                console.log(response);
            }
        });
}
function novoucher_savecustomquestion(){
    $.ajax({
            type : 'POST',
            url : 'customquestion.php',
            data:{name:$('#uname').val(),email_address:$('#email').val(),question:$('#customquestion').val()},
            success: function(response){
                window.location.replace("http://simple.comeze.com");
            },
            error : function(response){
                console.log(response);
            }
        });
}
function justdoit(){
    console.log("yo2");
    if($('#customquestion').length>0){
//        console.log("You cannot make more custom questions");
        alert("Uw vraag in het juiste veld");
    }    
    else
    {
        $('.box').append('<textarea id="customquestion" cols="5" rows="5" style="width:100%" name="customquestion"></textarea><button type="button" id="customquestionbutton" class="span2" onclick="savecustomquestion()">Verstuur</button>');
    }        
};
$('#voucher_code').focusout(function(){
    $.ajax({
        type:"POST",
        url:"dbcheck.php",
        data:{dataoption:'voucher',voucher:($('#voucher_code').val()),email:($('#email').val())},
        timeout:4000,
        beforeSend : function(){
            $('#ajaximg').show();
        },
        success : function(response){
            if(response.toString()!='false')
            {   
                $('#ajaximg').hide();
                $('#question1,#question2,#box1,#msg1').remove();
                $('.box').append('<div id="question1"><label for="q1">Selecteer uw vraag:</label><select class="input-xxlarge" id="q1" name="q1"><option value="0">maak uw keus</option>');
                var rows     = response.split('"');
                for(var j=1;j<rows.length;j++)
                {
                    if(j%2!==0)
                    {
                        var rowpieces = rows[j];
                        //console.log(rowpieces);
                        var finalrow  = rowpieces.split(',');
                        {
                            //console.log(finalrow);
                            $('#q1').append("<option data-help='"+finalrow[2]+"' value='"+finalrow[0]+"'>"+finalrow[1]+"</option>");
                        }
                    }
                }
            }
            else
            {
                $('#question1,#question2,#box1,#msg1').remove();
            }
        },
        error   : function (response){
            console.log(response);
        }
    });
});
$(document).on('change','#q1',function(){
    $.ajax({
        type:"POST",
        datatype: 'html',
        url:"dbcheck.php",
        data:{dataoption:'q1',option1:($('#q1 option:selected').val()),voucher_code:$('#voucher_code').val(),table_name:$('#q1 option:selected').attr('data-help')},
        success : function(response){
            if(response.toString()!='false')
            {
                $('#question2,#box1,#msg1').remove();
/*                var answeroption = "";
                if(answeroption == 'dropdown')
                {
                    $('.box').append('<div class="control-group" id="question2"><label class="control-label" for="q2">Selecteer uw vraag:</label><div class="controls"><select class="input-xxlarge" id="q2" name="q2">');
                    var rows      = response.split('"');
                    for(var j=1;j<rows.length;j++)
                    {
                        if(j%2!==0)
                        {
                            var rowpieces = rows[j];
                            var finalrow  = rowpieces.split(',');
                            {
                               $('#q2').append("<option value='"+finalrow[0]+"'>"+finalrow[1]+"</option>");
                            }
                        }
                    }*/
                if($('#openform').length>0)
                    $('#openform').remove();    
                $('.box').append(response+'<button id="openform" onclick="justdoit()" >andere vraag</button>');
            }
/*                else if(response.toString()=='box')
                {
                    $('.box').append('<div class="answer" id="answer"></div>');
                }
                else if(response.toString()=='email')
                {
                    $('.box').append('<div class="answer" id="answer"></div>');
                }
            }*/
            else
            {
                $('#question2,#box1,#msg1,#openform').remove();
            }
        },
        error   : function (response){
            console.log(response);
        }
        
    });
});
$(document).on('change','#q2',function(){
    if($(this).val()=="0")
    {
        $('.box').append('<textarea id="customquestion" cols="5" rows="5" style="width:100%" name="customquestion"></textarea><button type="button" id="customquestionbutton" class="span2">Verstuur</button>');
    }
    else{
            $('#customquestionform').remove();
            $.ajax({
            type:"POST",
            url:"dbcheck.php",
            datatype:'html',
            data:{dataoption:'q2',option2:($('#q2 option:selected').val()),voucher_code:$('#voucher_code').val(),table_name:$('#q1 option:selected').attr('data-help')},
            success : function(response){
                if(response.toString()!='false')
                {
                    $('#msg1').remove();
    //                if(response.toString()=='"box"')
    //                {
                        //alert(response.toString());
                        // console.log("yo1");
                        if($('#openform').length>0)
                            $('#openform').remove();
                        $('.box').append(response +'<button id="openform" onclick="justdoit()">andere vraag</button>');
    //                }
    //                else if(response.toString()=='"email"')
    //                {
    //                    $('.box').append('<div id="msg1">Email sent</div>');
    //                }
                }
                else
                {
                    $('#box1,#msg1,#openform').remove();
                }
            }
        });
    }
});
$('#novoucher').on('change',function(){
    if(this.checked){
        $('.box').append('<textarea id="customquestion" cols="5" rows="5" style="width:100%" name="customquestion"></textarea><button type="button" id="customquestionbutton" class="span2" onclick="novoucher_savecustomquestion()">Verstuur</button>');
    }
    else{
        if($('#voucherp').length>0){
            $('#customquestion,#customquestionbutton').remove();
        }
        else{
            // console.log("nothing");
        }
    }
});